package cn.itcast.bos.web.action.qp;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.crm.service.customer.CustomerService;
import cn.itcast.bos.domain.bc.customer.Customer;
import cn.itcast.bos.domain.qp.NoticeBill;
import cn.itcast.bos.domain.user.User;
import cn.itcast.bos.web.action.base.BaseAction;

@Controller("noticeBillAction")
@Scope("prototype")
@Namespace("/qp")
@ParentPackage("bos")
public class NoticeBillAction extends BaseAction<NoticeBill> {

	@Autowired
	// 注入报错... 启动容器报错... STS Version: 3.6.3.RELEASE 业务注入 cxf 业务层 CustomerService 需要 action 注入
	private CustomerService customerService;

	@Action(value = "noticeBillAction_add", results = { @Result(name = "noticeBillAction_add", location = "/WEB-INF/pages/qupai/noticebill_add.jsp") })
	public String add() {
		// 1: 接受 客户端 表单信息 model 没有省市区地址 北京市北京市朝阳区 河北省石家庄市xx区
		String province = (String) getParameter("province");
		String city = (String) getParameter("city");
		String district = (String) getParameter("district");
		String detail = (String) getParameter("detail");
		model.setPickaddress(province + city + district + detail);// 业务通知单 数据录入准备
		// 首先登陆 然后 将当前用户封装 model 中
		// User loginUser = (User) getSessionData("existUser");// 当前登陆的用户
		Subject subject = SecurityUtils.getSubject();// 当前用户
		User loginUser = (User) subject.getPrincipal();// 当前登陆的用户
		if (loginUser == null) {
			return "no_login";
		}
		model.setUser(loginUser);
		// 获取crm 系统 查询 客户
		Customer c = customerService.findCustomerByAddress(model.getPickaddress());
		// 2: 调用业务层 noticeBillService add/save 实现自动分单 连接crm 系统 需要CustomerService
		// 1: crm 系统地址完全匹配findCustomerByPickaddress---->Customer 对象 null ? 是否执行第二种 自动分单方案?
		noticeBillService.add(model, province, city, district, detail, c);
		// 2: 根据省市区 查询 所有分区 ---> 关键模糊匹配 --->分区 ---> 定区 --->staff
		// findRegionByPandCityanddo--->Region---> subareas--->循环 通过关键字

		return "noticeBillAction_add";
	}
}
